#include "polar_coordinates.h"

polar_coordinates::polar_coordinates()
{
	Center = Point2f(-1, -1);
}

polar_coordinates::polar_coordinates(Point2f CenterPoint)
{
	Center = CenterPoint;
}

void polar_coordinates::set_center(Point2f CenterPoint)
{
	Center = CenterPoint;
}

PointPolar polar_coordinates::Cartesian2Polar(Point2f Cartesian)
{
	float x0 = Center.x;
	float x = Cartesian.x;
	float y0 = Center.y;
	float y = Cartesian.y;
	float length = 0;
	float angle = 0;
	float angle_sin = 0;
	float angle_cos = 0;
	length = sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0));
	angle_cos = (x - x0) / length;
	angle_sin = (y - y0) / length;
	if (angle_cos >= 0 && angle_sin >= 0)
	{
		angle = asin(angle_sin) / pi * 180;
	}
	else if (angle_cos >= 0 && angle_sin <= 0)
	{
		angle = 360 - acos(angle_cos) / pi * 180;
	}
	else if (angle_cos <= 0 && angle_sin >= 0)
	{
		angle = 180 - asin(angle_sin) / pi * 180;
	}
	else if (angle_cos <= 0 && angle_sin <= 0)
	{
		angle = 360 - acos(angle_cos) / pi * 180;
	}
	PointPolar _return;
	_return.angle = angle;
	_return.length = length;
	return _return;
}

Point2f polar_coordinates::Polar2Cartesian(PointPolar Polar)
{
	float x;
	float y;
	x = Polar.length * cos(Polar.angle / 180 * pi) + Center.x;
	y = Polar.length * sin(Polar.angle / 180 * pi) + Center.y;
	Point2f _return;
	_return.x = x;
	_return.y = y;
	return _return;
}

